/*=========================================================================
| KCalDAV
|--------------------------------------------------------------------------
| (c) 2009  Kumaran Santhanam
|
| This project is released under the GNU General Public License.
| Please see the file COPYING for more details.
|--------------------------------------------------------------------------
| Remote calendar loading.
 ========================================================================*/

/*=========================================================================
| INCLUDES
 ========================================================================*/

#include "reader.h"
#include <kdebug.h>

/*=========================================================================
| NAMESPACE
 ========================================================================*/

using namespace KCal;

/*=========================================================================
| METHODS
 ========================================================================*/

void CalDavReader::cleanJob() {
    CalDavJob::cleanJob();
    mData = "";
}

int CalDavReader::runJob(runtime_info* RT) {
    kDebug() << "reader::run, url: " << url();

    response* result = caldav_get_response();
    CALDAV_RESPONSE res = OK;

    if (mGetAll) {
        kDebug() << "getting all objects";
        res = caldav_getall_object(result, url().toStdString().c_str(), RT);
    } else {
        kDebug() << "getting object from the specified time range";
        res = caldav_get_object(result, mTimeStart.toTime_t(), mTimeEnd.toTime_t(), url().toStdString().c_str(), RT);
    }

    if (OK == res) {
        kDebug() << "success";
        if (result->msg) {
            if (mEncoding == NULL) {
                kDebug() << "get locale encoding for decode";
                mEncoding = QTextCodec::codecForLocale()->name();
            }

            kDebug() << "encoding: " << mEncoding;
            QTextCodec* codec = QTextCodec::codecForName(mEncoding.toAscii());
            mData = codec->toUnicode(result->msg);
        } else {
            kDebug() << "empty collection";
            // empty collection
            mData = "";
        }
    }

    caldav_free_response(&result);

    return res;
}

// EOF ========================================================================
